package xyz.playedu.resource.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.playedu.common.constant.BackendConstant;
import xyz.playedu.common.domain.AdminRole;
import xyz.playedu.common.domain.AdminRoleCategories;
import xyz.playedu.common.service.AdminRoleCategoriesService;
import xyz.playedu.common.service.AdminRoleService;
import xyz.playedu.common.service.AdminUserService;
import xyz.playedu.common.types.CategoriesTree;
import xyz.playedu.resource.domain.ResourceCategories;
import xyz.playedu.resource.mapper.ResourceCategoriesMapper;
import xyz.playedu.resource.service.ResourceCategoriesService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author chenguiying
 * @since 2024/7/16 16:03
 */
@Service
public class ResourceCategoriesServiceImpl
        extends ServiceImpl<ResourceCategoriesMapper, ResourceCategories>
        implements ResourceCategoriesService {

    @Autowired private AdminRoleService adminRoleService;

    @Autowired private AdminUserService adminUserService;

    @Autowired private AdminRoleCategoriesService roleCategoriesService;

    @Override
    public List<CategoriesTree> getCategoriesTree() {
        return this.baseMapper.getCategoriesTree();
    }

    private List<Integer> getIdsAndParentIdsByIds(List<Integer> ids) {
        List<Integer> list = new ArrayList<>();
        if (ObjectUtil.isEmpty(ids)) {
            return list;
        }
        List<ResourceCategories> resourceCategoriesList = list(query().getWrapper().in("id", ids));
        for (ResourceCategories resourceCategories : resourceCategoriesList) {
            buildList(list, resourceCategories);
        }

        return list;
    }

    private void buildList(List<Integer> list, ResourceCategories resourceCategories) {
        String parentChain = resourceCategories.getParentChain();
        if (!list.contains(resourceCategories.getId())) {
            list.add(resourceCategories.getId());
        }
        if (ObjectUtil.isNotEmpty(parentChain)) {
            String[] split = parentChain.split(",");
            for (String s : split) {
                if (!list.contains(s)) {
                    list.add(Integer.valueOf(s));
                }
            }
        }
    }

    @Override
    public List<Integer> getCategoriesAndParentByUserId(Integer userId) {
        // 读取超级管理角色
        AdminRole superRole = adminRoleService.getBySlug(BackendConstant.SUPER_ADMIN_ROLE);

        List<Integer> ids = new ArrayList<>();
        List<Integer> roleIds = adminUserService.getRoleIdsByUserId(userId);
        if (roleIds.size() == 0) {
            return ids;
        }

        if (roleIds.contains(superRole.getId())) { // 包含超级管理角色的话返回全部权限
            ids = list().stream().map(ResourceCategories::getId).toList();
        } else { // 根据相应的roleIds读取权限
            List<AdminRoleCategories> roleCategoriesList = roleCategoriesService.list(roleCategoriesService.query().getWrapper().in("role_id", roleIds));
            if (ObjectUtil.isNotEmpty(roleCategoriesList)) {
                List<Integer> categoriesIds = roleCategoriesList.stream().map(AdminRoleCategories::getCategoriesId).toList();
                ids = getIdsAndParentIdsByIds(categoriesIds);
            }
        }
        return ids;
    }

    @Override
    public List<Integer> getCategoriesByUserId(Integer userId) {
        // 读取超级管理角色
        AdminRole superRole = adminRoleService.getBySlug(BackendConstant.SUPER_ADMIN_ROLE);

        List<Integer> ids = new ArrayList<>();
        List<Integer> roleIds = adminUserService.getRoleIdsByUserId(userId);
        if (roleIds.size() == 0) {
            return ids;
        }

        if (roleIds.contains(superRole.getId())) { // 包含超级管理角色的话返回全部权限
            ids = list().stream().map(ResourceCategories::getId).toList();
        } else { // 根据相应的roleIds读取权限
            List<AdminRoleCategories> roleCategoriesList = roleCategoriesService.list(roleCategoriesService.query().getWrapper().in("role_id", roleIds));
            if (ObjectUtil.isNotEmpty(roleCategoriesList)) {
                ids = roleCategoriesList.stream().map(AdminRoleCategories::getCategoriesId).toList();
            }
        }
        return ids;
    }
}
